InfluxDB stock data fits naturally into time-series systems because stock prices and trading volumes change continuously over time. Developers often choose InfluxDB when they need to store and analyze metrics such as monitoring data, sensor readings, or application performance indicators. Stock data follows the same pattern.
In this tutorial, we use a Python web crawler to retrieve stock data, write it into InfluxDB, and query it efficiently for analysis.
Why InfluxDB Is Suitable for Stock Time-Series Data
CoInfluxDB focuses on timestamped data. Unlike traditional relational databases, it optimizes storage and queries for time-based access patterns. As a result, it handles sequential stock records efficiently.
Developers commonly apply InfluxDB to scenarios such as:
- Server monitoring: CPU usage, memory usage, network traffic
- IoT systems: temperature, humidity, device status
- Application metrics: API latency, error rate, request volume
Stock prices share these characteristics. Therefore, teams often rely on InfluxDB to store and analyze stock market data.
InfluxDB vs MySQL for Time-Series Workloads
| Scenario | InfluxDB | MySQL |
|---|---|---|
| High-frequency writes | Designed for concurrent writes | Requires sharding or buffering |
| Time-window aggregation | Built-in time functions | Complex SQL queries |
| Long-term historical data | Compression-friendly | Storage grows quickly |
| Real-time monitoring | Time-series native | External tools required |
Install InfluxDB on Ubuntu
InfluxDB supports Linux, macOS, and Windows. In this example, Ubuntu is used.
wget https://dl.influxdata.com/influxdb/releases/influxdb_1.8.10_amd64.deb
sudo dpkg -i influxdb_1.8.10_amd64.deb
sudo systemctl start influxdb
Verify installation:
influx -version
Create a Database and Connect
CREATE DATABASE db_stock;
SHOW DATABASES;
InfluxDB also exposes an HTTP API on port 8086, which allows database operations through REST requests.
At the same time, measurements, tags, and fields are created dynamically when data is written—no schema is required in advance.
Python Web Crawler to Fetch Stock Data
We retrieve U.S. stock data from a public financial source:
- Source: https://finance.sina.com.cn/stock/usstock/sector.shtml
Using Python, the crawler fetches historical data and converts it into a DataFrame.
For example, the following code retrieves NVIDIA (NVDA) stock data:
def fetch_stock_data(ticker):
df = stock_us_daily(symbol=ticker)
print(df.tail())
return df
fetch_stock_data("NVDA")
Write Stock Data into InfluxDB
Once the data is collected, it is written to InfluxDB as time-series points.
point = {
"measurement": "stock_data",
"time": row["date"],
"tags": {
"ticker": "NVDA"
},
"fields": {
"open": row["open"],
"close": row["close"],
"high": row["high"],
"low": row["low"],
"volume": row["volume"]
}
}
Here, tags are used for classification (such as stock symbol), while fields store numeric values.
Tags vs Fields Explained Simply
Although both store information, tags and fields serve different purposes:
| Feature | Tag | Field |
|---|---|---|
| Data type | String only | Numeric, string, boolean |
| Indexed | Yes | No |
| Purpose | Filtering & grouping | Actual values |
| Query speed | Faster | Slower |
Therefore, stock symbols are ideal tags, while prices and volume belong in fields.
Query Data from InfluxDB
SELECT * FROM stock_data WHERE ticker = 'NVDA' ORDER BY time DESC;
The syntax is very similar to SQL, which makes InfluxDB easy to learn for developers with MySQL experience.
Calculate Moving Average with Python
After querying the data, Python can be used to compute indicators:
df['ma5'] = df['close'].rolling(window=5).mean()
As a result, you can quickly generate technical indicators without complex database queries.
Typical Use Cases After Data Storage
Once the stock data is stored, you can:
- Run basic time-based queries
- Calculate indicators like MA and MACD
- Visualize trends using Grafana
- Set up real-time alerts
- Optimize performance with pre-aggregation
Conclusion
Storing stock time-series data in InfluxDB provides a clean and efficient workflow. By combining Python and InfluxDB, you can collect, store, and analyze stock data with minimal overhead.
In the next step, this data can be visualized and monitored in real time.